[id].vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div class="admin--event-form">
  3. <div v-if="isLoading" class="admin--loading">
  4. 데이터를 불러오는 중...
  5. </div>
  6. <form v-else @submit.prevent="handleSubmit" class="admin--form">
  7. <!-- 카테고리 -->
  8. <div class="admin--form-group">
  9. <label class="admin--form-label">카테고리 <span class="admin--required">*</span></label>
  10. <select v-model="formData.category" class="admin--form-select" required>
  11. <option value="">카테고리를 선택하세요</option>
  12. <option value="진행중">진행중</option>
  13. <option value="완료">완료</option>
  14. </select>
  15. </div>
  16. <!-- 댓글허용 -->
  17. <div class="admin--form-group">
  18. <label class="admin--form-label">댓글허용</label>
  19. <div class="admin--checkbox-group">
  20. <label class="admin--checkbox-label">
  21. <input v-model="formData.allow_comment" type="checkbox">
  22. <span>댓글 허용</span>
  23. </label>
  24. </div>
  25. </div>
  26. <!-- 공지 -->
  27. <div class="admin--form-group">
  28. <label class="admin--form-label">공지</label>
  29. <div class="admin--checkbox-group">
  30. <label class="admin--checkbox-label">
  31. <input v-model="formData.is_notice" type="checkbox">
  32. <span>공지글로 등록</span>
  33. </label>
  34. </div>
  35. </div>
  36. <!-- 이름 -->
  37. <div class="admin--form-group">
  38. <label class="admin--form-label">이름 <span class="admin--required">*</span></label>
  39. <input
  40. v-model="formData.name"
  41. type="text"
  42. class="admin--form-input"
  43. placeholder="이름을 입력하세요"
  44. required
  45. >
  46. </div>
  47. <!-- 이메일 -->
  48. <div class="admin--form-group">
  49. <label class="admin--form-label">이메일 <span class="admin--required">*</span></label>
  50. <input
  51. v-model="formData.email"
  52. type="email"
  53. class="admin--form-input"
  54. placeholder="이메일을 입력하세요"
  55. required
  56. >
  57. </div>
  58. <!-- 기간 -->
  59. <div class="admin--form-group">
  60. <label class="admin--form-label">기간 <span class="admin--required">*</span></label>
  61. <div class="admin--date-range">
  62. <DatePicker
  63. v-model="formData.start_date"
  64. placeholder="시작일 선택"
  65. :max-date="formData.end_date"
  66. required
  67. />
  68. <span class="admin--date-separator">~</span>
  69. <DatePicker
  70. v-model="formData.end_date"
  71. placeholder="종료일 선택"
  72. :min-date="formData.start_date"
  73. required
  74. />
  75. </div>
  76. </div>
  77. <!-- 제목 -->
  78. <div class="admin--form-group">
  79. <label class="admin--form-label">제목 <span class="admin--required">*</span></label>
  80. <input
  81. v-model="formData.title"
  82. type="text"
  83. class="admin--form-input"
  84. placeholder="제목을 입력하세요"
  85. required
  86. >
  87. </div>
  88. <!-- 내용 -->
  89. <div class="admin--form-group">
  90. <label class="admin--form-label">내용 <span class="admin--required">*</span></label>
  91. <SunEditor v-model="formData.content" />
  92. </div>
  93. <!-- 기존 첨부파일 -->
  94. <div v-if="existingFiles.length > 0" class="admin--form-group">
  95. <label class="admin--form-label">기존 첨부파일</label>
  96. <div class="admin--file-list">
  97. <div v-for="(file, index) in existingFiles" :key="'existing-' + index" class="admin--file-item">
  98. <a :href="file.url" target="_blank" class="admin--file-name">{{ file.name }}</a>
  99. <span class="admin--file-size">({{ formatFileSize(file.size) }})</span>
  100. <button type="button" class="admin--btn-remove-file" @click="removeExistingFile(index)">
  101. 삭제
  102. </button>
  103. </div>
  104. </div>
  105. </div>
  106. <!-- 새 파일첨부 -->
  107. <div class="admin--form-group">
  108. <label class="admin--form-label">파일첨부</label>
  109. <div v-if="attachedFiles.length > 0" class="admin--file-list">
  110. <div v-for="(file, index) in attachedFiles" :key="'new-' + index" class="admin--file-item">
  111. <span class="admin--file-name">{{ file.name }}</span>
  112. <span class="admin--file-size">({{ formatFileSize(file.size) }})</span>
  113. <button type="button" class="admin--btn-remove-file" @click="removeFile(index)">
  114. 삭제
  115. </button>
  116. </div>
  117. </div>
  118. <input
  119. ref="fileInput"
  120. type="file"
  121. multiple
  122. class="admin--form-file-hidden"
  123. @change="handleFileAdd"
  124. >
  125. <button type="button" class="admin--btn admin--btn-secondary" @click="triggerFileInput">
  126. 파일 추가
  127. </button>
  128. </div>
  129. <!-- 버튼 영역 -->
  130. <div class="admin--form-actions">
  131. <button
  132. type="submit"
  133. class="admin--btn admin--btn-primary"
  134. :disabled="isSaving"
  135. >
  136. {{ isSaving ? '저장 중...' : '확인' }}
  137. </button>
  138. <button
  139. type="button"
  140. class="admin--btn admin--btn-secondary"
  141. @click="goToList"
  142. >
  143. 목록
  144. </button>
  145. </div>
  146. <!-- 성공/에러 메시지 -->
  147. <div v-if="successMessage" class="admin--alert admin--alert-success">
  148. {{ successMessage }}
  149. </div>
  150. <div v-if="errorMessage" class="admin--alert admin--alert-error">
  151. {{ errorMessage }}
  152. </div>
  153. </form>
  154. </div>
  155. </template>
  156. <script setup>
  157. import { ref, onMounted } from 'vue'
  158. import { useRoute, useRouter } from 'vue-router'
  159. import SunEditor from '~/components/admin/SunEditor.vue'
  160. import DatePicker from '~/components/admin/DatePicker.vue'
  161. definePageMeta({
  162. layout: 'admin',
  163. middleware: ['auth']
  164. })
  165. const route = useRoute()
  166. const router = useRouter()
  167. const { get, put, upload } = useApi()
  168. const isLoading = ref(true)
  169. const isSaving = ref(false)
  170. const successMessage = ref('')
  171. const errorMessage = ref('')
  172. const attachedFiles = ref([])
  173. const existingFiles = ref([])
  174. const fileInput = ref(null)
  175. const formData = ref({
  176. category: '',
  177. allow_comment: false,
  178. is_notice: false,
  179. name: '',
  180. email: '',
  181. start_date: '',
  182. end_date: '',
  183. title: '',
  184. content: '',
  185. file_urls: []
  186. })
  187. const loadEvent = async () => {
  188. isLoading.value = true
  189. const id = route.params.id
  190. const { data, error } = await get(`/board/event/${id}`)
  191. console.log('[EventEdit] 데이터 로드:', { data, error })
  192. if (data?.success && data?.data) {
  193. const event = data.data
  194. formData.value = {
  195. category: event.category || '',
  196. allow_comment: Boolean(event.allow_comment),
  197. is_notice: Boolean(event.is_notice),
  198. name: event.name || '',
  199. email: event.email || '',
  200. start_date: event.start_date || '',
  201. end_date: event.end_date || '',
  202. title: event.title || '',
  203. content: event.content || '',
  204. file_urls: event.file_urls || []
  205. }
  206. existingFiles.value = event.file_urls || []
  207. console.log('[EventEdit] 로드 성공')
  208. }
  209. isLoading.value = false
  210. }
  211. const triggerFileInput = () => {
  212. fileInput.value?.click()
  213. }
  214. const handleFileAdd = (event) => {
  215. const files = Array.from(event.target.files)
  216. attachedFiles.value.push(...files)
  217. event.target.value = ''
  218. }
  219. const removeFile = (index) => {
  220. attachedFiles.value.splice(index, 1)
  221. }
  222. const removeExistingFile = (index) => {
  223. existingFiles.value.splice(index, 1)
  224. }
  225. const formatFileSize = (bytes) => {
  226. if (bytes === 0) return '0 Bytes'
  227. const k = 1024
  228. const sizes = ['Bytes', 'KB', 'MB', 'GB']
  229. const i = Math.floor(Math.log(bytes) / Math.log(k))
  230. return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
  231. }
  232. const handleSubmit = async () => {
  233. successMessage.value = ''
  234. errorMessage.value = ''
  235. if (!formData.value.category) {
  236. errorMessage.value = '카테고리를 선택하세요.'
  237. return
  238. }
  239. if (!formData.value.title) {
  240. errorMessage.value = '제목을 입력하세요.'
  241. return
  242. }
  243. if (!formData.value.content) {
  244. errorMessage.value = '내용을 입력하세요.'
  245. return
  246. }
  247. if (!formData.value.start_date || !formData.value.end_date) {
  248. errorMessage.value = '기간을 입력하세요.'
  249. return
  250. }
  251. isSaving.value = true
  252. try {
  253. let fileUrls = [...existingFiles.value]
  254. // 새 파일 업로드
  255. if (attachedFiles.value.length > 0) {
  256. for (const file of attachedFiles.value) {
  257. const formDataFile = new FormData()
  258. formDataFile.append('file', file)
  259. const { data: uploadData, error: uploadError } = await upload('/upload/event-file', formDataFile)
  260. if (uploadError) {
  261. errorMessage.value = `파일 업로드에 실패했습니다: ${file.name}`
  262. isSaving.value = false
  263. return
  264. }
  265. if (!uploadData?.success || !uploadData?.data?.url) {
  266. errorMessage.value = '파일 업로드 응답이 올바르지 않습니다.'
  267. isSaving.value = false
  268. return
  269. }
  270. fileUrls.push({
  271. name: file.name,
  272. url: uploadData.data.url,
  273. size: file.size
  274. })
  275. }
  276. }
  277. // content에서 도메인 제거
  278. let contentToSave = formData.value.content
  279. if (contentToSave) {
  280. // http://도메인 또는 https://도메인 제거
  281. contentToSave = contentToSave.replace(/https?:\/\/[^\/]+/g, '')
  282. }
  283. const submitData = {
  284. ...formData.value,
  285. content: contentToSave,
  286. file_urls: fileUrls
  287. }
  288. const id = route.params.id
  289. const { data, error } = await put(`/board/event/${id}`, submitData)
  290. if (error) {
  291. errorMessage.value = error.message || '수정에 실패했습니다.'
  292. } else {
  293. successMessage.value = '이벤트가 수정되었습니다.'
  294. setTimeout(() => {
  295. router.push('/admin/board/event')
  296. }, 1000)
  297. }
  298. } catch (error) {
  299. errorMessage.value = '서버 오류가 발생했습니다.'
  300. console.error('Save error:', error)
  301. } finally {
  302. isSaving.value = false
  303. }
  304. }
  305. const goToList = () => {
  306. router.push('/admin/board/event')
  307. }
  308. onMounted(() => {
  309. loadEvent()
  310. })
  311. </script>